package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;

import com.hongxun.design.R;
import com.hongxun.design.bean.BitTypeTabItemBean;

import com.hongxun.design.mytools.MString;
import com.hongxun.design.utils.imageutil.UriUtil;

/**
 * Created by zerdoor_pc on 2016/4/27.
 */
public class BitTypeTabGridAdapter extends BaseAdapter {
    List<BitTypeTabItemBean> itemBeanList;
    Context context;

    public BitTypeTabGridAdapter(List<BitTypeTabItemBean> itemBeanList, Context context) {
        this.itemBeanList = itemBeanList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return itemBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (convertView == null)
            convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item_bittype, null);
        BitTypeTabItemBean itemBean = itemBeanList.get(i);
        ViewHolerHelper viewHolerHelper = new ViewHolerHelper(convertView);
        RelativeLayout tab_item = viewHolerHelper.getView(R.id.bittype_tab_item);
        SimpleDraweeView tab_icon = viewHolerHelper.getView(R.id.bittype_tab_icon);
        TextView tab_name = viewHolerHelper.getView(R.id.bittype_tab_name);

        tab_icon.setImageURI(UriUtil.getUri(MString.LOCALIMAGEHEAD+itemBean.getIcon()));
        tab_name.setText(itemBean.getName());
        tab_item.setOnClickListener(itemBean.getListener());
        return convertView;
    }

    public class ViewHolerHelper {
        private HashMap<Integer, View> mViews;
        View convertView;

        public ViewHolerHelper(View convertView) {
            mViews = new HashMap<Integer, View>();
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item_home_head, null);
            }
            convertView.setTag(this);
            this.convertView = convertView;
        }

        public <T> T getView(Integer R_Id) {
            View view = null;
            if (!mViews.containsKey(R_Id)) {
                view = convertView.findViewById(R_Id);
                if (view != null) {
                    mViews.put(R_Id, view);
                }
            } else {
                view = mViews.get(R_Id);
            }
            return (T) view;
        }
    }

}
